package org.freespace.controller;


import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.freespace.Constant;
import org.freespace.model.Environment;
import org.freespace.model.EnvironmentMachine;
import org.freespace.model.JmxPlugin;
import org.freespace.model.JmxScript;
import org.freespace.service.IEnvironmentMachineService;
import org.freespace.service.IEnvironmentService;
import org.freespace.service.IJmxScriptService;
import org.freespace.utils.SessionUtil;
import org.freespace.utils.response.CommonResult;
import org.freespace.vo.EnvironmentVO;
import org.freespace.vo.JmxScriptVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuqi
 * @since 2023-05-29
 */
@Slf4j
@RestController
@RequestMapping("/environment")
public class EnvironmentController {

    @Autowired
    IEnvironmentService service;


    @Autowired
    IEnvironmentMachineService envMachineService;
    @PostMapping(value = "list")
    public CommonResult list(@RequestBody EnvironmentVO vo) {
        IPage<Environment> page = new Page<>(vo.getCurrentPage(), vo.getPageSize());
        Wrapper<Environment> wrapper = Wrappers.query();
        if(StringUtils.isNotBlank(vo.getCondition())) {
            wrapper = Wrappers.<Environment>lambdaQuery()
                    .like(Environment::getId, vo.condition)
                    .or().like(Environment::getEnvName, vo.condition)
                    .or().like(Environment::getCreateBy, vo.condition);
        }
        IPage<Environment> data = service.page(page, wrapper);
        log.info("{}", data.toString());
        return CommonResult.success(data);
    }

    @PostMapping("save")
    public CommonResult save(@RequestPart("envInfo") Environment envInfo) throws IOException {
        envInfo.setCreateBy(SessionUtil.getCurrentUser());
        envInfo.setCreateTime(LocalDateTime.now());
        envInfo.setLastUpdateBy(SessionUtil.getCurrentUser());
        envInfo.setLastUpdateTime(LocalDateTime.now());
        service.save(envInfo);
        return CommonResult.success();
    }

    @PostMapping(value = "machine/list")
    public CommonResult machineList(@RequestBody EnvironmentVO vo) {
        IPage<EnvironmentMachine> page = new Page<>(vo.getCurrentPage(), vo.getPageSize());
        LambdaQueryWrapper<EnvironmentMachine> wrapper = Wrappers.<EnvironmentMachine>lambdaQuery()
                .eq(EnvironmentMachine::getEnvId, vo.getEnvId());
        IPage<EnvironmentMachine> data = envMachineService.page(page, wrapper);
        log.info("{}", data.toString());
        return CommonResult.success(data);
    }

    @PostMapping(value = "machine/add")
    public CommonResult machineAdd(@RequestBody EnvironmentMachine vo) {
        envMachineService.save(vo);
        return CommonResult.success();
    }
    @GetMapping("delete/{id}")
    public CommonResult delete(@PathVariable("id") int id) {
        service.removeById(id);
        envMachineService.removeById(id);
        return CommonResult.success();
    }
}
